#import hashlib
import base64
from lib.scrypto import scrypto
from config import config

# -*- coding: UTF-8 -*-
__license__ = "GPL http://www.gnu.org/licenses/gpl.txt"
__author__="xushengs@gmail.com"
__homepage__ = "http://fdream.net"
__date__ ="2010-2-26 17:39:27"
__version = "0.1"

class usercookie:
    @classmethod
    def encrypt(self, tokens):
            name = scrypto.encrypt(tokens['screen_name'], config.encrypt_key)
            token = scrypto.encrypt(tokens['oauth_token'], config.encrypt_key)
            secret = scrypto.encrypt(tokens['oauth_token_secret'], config.encrypt_key)
            udi = name + '|' + token + '|' + secret
            return base64.encodestring(udi).replace('\n', '')

    @classmethod
    def decrypt(self, str):
        udi = base64.decodestring(str)
        (name, token, secret) = udi.split('|')
        tokens = {}
        tokens['screen_name'] = scrypto.decrypt(name, config.encrypt_key)
        tokens['oauth_token'] = scrypto.decrypt(token, config.encrypt_key)
        tokens['oauth_token_secret'] = scrypto.decrypt(secret, config.encrypt_key)
        return tokens

'''
if __name__ == "__main__":
    s = usercookie.encrypt('xushengs', 'pplive')
    d = usercookie.decrypt(s)
    if 'error' in d:
        print d['error']
    else:
        print d['name']
        print d['pwd']
'''